package com.mutual.product.service.impl;

import java.io.File;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mutual.attachment.pojo.Attachment;
import com.mutual.attachment.service.AttachmentService;
import com.mutual.commom.pojo.ReturnResult;
import com.mutual.commom.util.SystemUtil;
import com.mutual.product.dao.ProductDao;
import com.mutual.product.pojo.Product;
import com.mutual.product.service.ProductService;
import org.slf4j.Logger;

@Service
public class ProductServiceImpl implements ProductService{

	@Resource
	private ProductDao productDao;
	
	@Resource
	private AttachmentService attachmentService;
	
	private static final Logger log = LoggerFactory.getLogger(ProductServiceImpl.class);
	
	@Override
	@Transactional
	public void insert(Product entity) {
		Date date=new Date();
		entity.setCreateDate(date);
		entity.setUpdateDate(date);
		productDao.insertSelective(entity);
//		Attachment attachment=new Attachment();
//		attachment.setId(id);
//		attachmentService.
		
	}

	@Override
	public void updateByPrimaryKey(Product entity) {
		entity.setUpdateDate(new Date());
		productDao.updateByPrimaryKeySelective(entity);
	}

	@Override
	public Product selectByPrimaryKey(Serializable entityid) {
		return productDao.selectByPrimaryKey(entityid);
	}

	@Override
	public int selectProductTotal(Map<String, Object> param) {
		return productDao.selectTotalCount(param);
	}

	@Override
	public List<Product> selectProductList(Map<String, Object> param) {
		if(param.containsKey("page")){
			int pageNo=SystemUtil.getPageNo(param.get("page"));
			int pageSize=Integer.parseInt(param.get("limit").toString());
			param.put("startNo", (pageNo-1)*pageSize);
			param.put("pageSize", pageSize);
		}
		return productDao.selectObjectList(param);
	}

	@Override
	@Transactional
	public ReturnResult addProduct(Product product, Attachment a) {
		Date date=new Date();
		product.setCreateDate(date);
		product.setUpdateDate(date);
		productDao.insertSelective(product);
		a.setMoudleId(product.getId());
		attachmentService.updateByPrimaryKey(a);
		ReturnResult returnResult=new ReturnResult();
		returnResult.setStatus(ReturnResult.STATUS_SUCCESS);
		return returnResult;
	}

	@Override
	@Transactional
	public ReturnResult updateProduct(Product product, Attachment a) {
		product.setUpdateDate(new Date());
		productDao.updateByPrimaryKeySelective(product);
		a.setMoudleId(product.getId());
		List<Attachment> attachments=attachmentService.selectAttachmentByMoudleId(a);
		if(attachments.isEmpty()||(!attachments.isEmpty()&&a.getId()!=attachments.get(0).getId())){
			attachmentService.updateByPrimaryKey(a);
		}
		if(!attachments.isEmpty()&&a.getId()!=attachments.get(0).getId()){
			attachmentService.deleteAttachment(attachments.get(0).getId());
			new File(attachments.get(0).getRealPathInfo()).delete();
			log.info("产品名称："+product.getName()+"删除附件信息");
		}
		ReturnResult returnResult=new ReturnResult();
		returnResult.setStatus(ReturnResult.STATUS_SUCCESS);
		return returnResult;
	}

	@Override
	@Transactional
	public int deleteProduct(int id) {
		Attachment attachment=new Attachment();
		attachment.setMoudleId(id);
		attachment.setType(Attachment.TYPE_PRODUCT);
		List<Attachment> attachments=attachmentService.selectAttachmentByMoudleId(attachment);
		attachments.forEach(item->{
			new File(item.getRealPathInfo()).delete();
			log.info("删除附件路径："+item.getRealPathInfo());
		});
		attachmentService.deleteAttachmentByMoudleId(attachment);
		productDao.deleteByPrimaryKey(id);
		return 0;
	}
}
